/************************************************************
 * Project Name		[JavaEE_XML]
 * File Name     	[StudentDAO_Dom4j.java]
 * Creation Date 	[09-08-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_exam.dao;

import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import pkg_exam.bean.Student;
import pkg_exam.exception.StudentNotFoundExcepiton;
import pkg_exam.util.StudentUtil_Dom4j;

/**
 * StudentDAO implemented by dom4j
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class StudentDAO_Dom4j {

	/**
	 * Add new student
	 * 
	 * @param student
	 */
	public void add(Student student) {
		try {
			Document document = StudentUtil_Dom4j.getDocument();

			Element studentElement = DocumentHelper.createElement("student");
			studentElement.addAttribute("studentId", student.getId());
			studentElement.addElement("name").setText(student.getName());
			studentElement.addElement("examID").setText(student.getExamId());
			studentElement.addElement("location")
					.setText(student.getLocation());
			studentElement.addElement("score").setText(
					String.valueOf(student.getScore()));

			document.getRootElement().add(studentElement);

			StudentUtil_Dom4j.writeToXML(document);

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Query student by ID
	 * 
	 * @param id
	 * @return
	 */
	public Student searchById(String id) {
		Student student = null;

		try {
			Document document = StudentUtil_Dom4j.getDocument();

			Element studentElement = (Element) document
					.selectSingleNode("//student[@studentId='" + id + "']");

			if (studentElement != null) {
				student = new Student();
				student.setId(id);
				student.setName(studentElement.element("name").getText());
				student.setExamId(studentElement.element("examID").getText());
				student.setLocation(studentElement.element("location")
						.getText());
				student.setScore(Double.parseDouble(studentElement.element(
						"score").getText()));
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return student;
	}

	/**
	 * Remove student by name
	 * 
	 * @param name
	 * @throws StudentNotFoundExcepiton
	 */
	@SuppressWarnings("rawtypes")
	public void deleteByName(String name) throws StudentNotFoundExcepiton {
		try {
			Document document = StudentUtil_Dom4j.getDocument();

			List nodes = document.selectNodes("//name");
			Iterator iter = nodes.iterator();

			while (iter.hasNext()) {
				Element nameElement = (Element) iter.next();
				if (name.equals(nameElement.getText())) {
					nameElement.getParent().getParent()
							.remove(nameElement.getParent());
					StudentUtil_Dom4j.writeToXML(document);
					return;
				}
			}
			throw new StudentNotFoundExcepiton(
					"Can not find student with name " + name);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
